var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
let wxhttp2 = function (newAxiosConfig) {
    let paramsDefaultConfig = {
        url: '',
        baseUrl: '',
        method: 'GET',
        timeout: 0,
        transformRequest: undefined,
        transformResponse: undefined,
        adapter: undefined,
        responseType: 'json',
        handleInterceptRequest: undefined,
        handleInterceptRequestErr: undefined,
        handleInterceptResponse: undefined,
        handleInterceptResponseErr: undefined
    };
    function http(params) {
        params = Object.assign({}, http.default, params);
    }
    // 设置默认值
    http.default = Object.assign({}, paramsDefaultConfig, (newAxiosConfig || {}));
    paramsDefaultConfig = null;
    // 请求的方法
    function request(config) {
        return __awaiter(this, void 0, void 0, function* () {
            if (config.handleInterceptRequest) {
                config = yield config.handleInterceptRequest(config);
            }
            config.baseUrl && (config.url = config.baseUrl + config.url);
            return new Promise((reso, rej) => {
                wx.request(Object.assign(Object.assign({}, config), { method: config.method.toUpperCase(), data: Object.assign(Object.assign({}, config.data), config.params), success(res) {
                        if (config.handleInterceptResponse) {
                            Promise.resolve(config.handleInterceptResponse(res)).then(res => {
                                reso(res);
                            }).catch(err => {
                                config.handleInterceptResponseErr && (config.handleInterceptResponseErr(err));
                            });
                        }
                        else {
                            reso(res);
                        }
                    },
                    fail(err) {
                        if (config.handleInterceptRequestErr) {
                            Promise.resolve(config.handleInterceptRequestErr(err)).then(res => {
                                rej(res);
                            });
                        }
                        else {
                            rej(err);
                        }
                    },
                    complete() {
                        console.log(`one request finished`);
                    } }));
            });
        });
    }
    // 请求方法的三种类型
    let requestMethods = {
        type1(url, config) {
            return request(Object.assign({}, http.default, (config || {}), { url, method: 'GET' }));
        },
        type2(type) {
            return function (url, config) {
                return request(Object.assign({}, http.default, (config || {}), { url, method: type.toUpperCase() }));
            };
        },
        type3(type) {
            return function (url, data, config) {
                return request(Object.assign({}, http.default, (config || {}), { url, method: type.toUpperCase(), data: data || {} }));
            };
        }
    };
    // 处理各个方法
    http.get = requestMethods.type1;
    http.delete = requestMethods.type2('delete');
    http.head = requestMethods.type2('head');
    http.options = requestMethods.type2('options');
    http.put = requestMethods.type3('put');
    http.post = requestMethods.type3('post');
    http.patch = requestMethods.type3('patch');
    // 设置拦截方法
    http.interceptors = {
        request: {
            use(handleRequest, handleError) {
                http.default.handleInterceptRequest = handleRequest;
                handleError && (http.default.handleInterceptRequestErr = handleError);
            }
        },
        response: {
            use(handleResponse, handleErr) {
                http.default.handleInterceptResponse = handleResponse;
                handleErr && (http.default.handleInterceptResponseErr = handleErr);
            }
        }
    };
    // 创建新的实例的方法
    http.create = wxhttp2;
    return http;
};
export default wxhttp2();
